package org.bluweb.g4wicket.language.keyboard;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.wicket.Component;
import org.apache.wicket.behavior.AttributeAppender;
import org.apache.wicket.markup.html.IHeaderResponse;
import org.apache.wicket.markup.html.form.AbstractTextComponent;
import org.apache.wicket.model.Model;
import org.bluweb.g4wicket.G4WicketBehaviour;
import org.bluweb.g4wicket.G4WicketEvent;


@SuppressWarnings("serial")
public class VirtualKeyboard extends G4WicketBehaviour {

	public final static String ALBANIAN = "sq";
	public final static String ARABIC = "ar";
	public final static String ARMENIAN_EASTERN = "hy_east";
	public final static String ARMENIAN_WESTERN = "hy_west";
	public final static String BASQUE = "eu";
	public final static String BELARUSIAN = "be";
	public final static String BENGALI_PHONETIC = "bn_phone";
	public final static String BOSNIAN = "bs";
	public final static String BRAZILIAN_PORTUGUESE = "pt_br";
	public final static String BULGARIAN = "bg";
	public final static String CATALAN = "ca";
	public final static String CROATIAN = "hr";
	public final static String CZECH = "cs";
	public final static String CZECH_QWERTZ = "cs_qwertz";
	public final static String DANISH = "da";
	public final static String DUTCH = "nl";
	public final static String DEVANAGARI_PHONETIC = "deva_phone";
	public final static String ENGLISH = "en";
	public final static String ESTONIAN = "et";
	public final static String ETHIOPIC = "ethi";
	public final static String FINNISH = "fi";
	public final static String FRENCH = "fr";
	public final static String GALICIAN = "gl";
	public final static String GEORGIAN_QWERTY = "ka_qwerty";
	public final static String GEORGIAN_TYPEWRITER = "ka_typewriter";
	public final static String GERMAN = "de";
	public final static String GREEK = "el";
	public final static String GUJARATI_PHONETIC = "gu_phone";
	public final static String GURMUKHI_PHONETIC = "guru_phone";
	public final static String HEBREW = "he";
	public final static String HINDI = "hi";
	public final static String HUNGARIAN_101 = "hu_101";
	public final static String ICELANDIC = "is";
	public final static String ITALIAN = "it";
	public final static String KANNADA_PHONETIC = "kn_phone";
	public final static String KAZAKH = "kk";
	public final static String KYRGYZ = "ky_cyrl";
	public final static String LITHUANIAN = "lt";
	public final static String LATVIAN = "lv";
	public final static String MACEDONIAN = "mk";
	public final static String MALAYALAM_PHONETIC = "ml_phone";
	public final static String MALTESE = "mt";
	public final static String MONGOLIAN_CYRILLIC = "mn_cyrl";
	public final static String MONTENEGRIN = "srp";
	public final static String NORWEGIAN = "no";
	public final static String ORIYA_PHONETIC = "or_phone";
	public final static String PERSIAN = "fa";
	public final static String POLISH = "pl";
	public final static String ROMANI = "rom";
	public final static String ROMANIAN = "ro";
	public final static String RUSSIAN = "ru";
	public final static String SANSKRIT_PHONETIC = "sa_phone";
	public final static String SERBIAN_CYRILLIC = "sr_cyrl";
	public final static String SERBIAN_LATIN = "sr_latn";
	public final static String SLOVAK = "sk";
	public final static String SLOVAK_QWERTY = "sk_qwerty";
	public final static String SLOVENIAN = "sl";
	public final static String SPANISH = "es_es";
	public final static String SWEDISH = "sv";
	public final static String TAMIL_PHONETIC = "ta_phone";
	public final static String TATAR = "tt";
	public final static String TELUGU_PHONETIC = "te_phone";
	public final static String THAI = "th";
	public final static String TURKISH_F = "tr_f";
	public final static String TURKISH_Q = "tr_q";
	public final static String UKRAINIAN_101 = "uk_101";
	public final static String UZBEK_LATIN = "uz_latn";
	public final static String UZBEK_CYRILLIC_PHONETIC = "uz_cyrl_phone";
	public final static String UZBEK_CYRILLIC_TYPEWRITTER = "uz_cyrl_type";

	private List<AbstractTextComponent<?>> textComponents = new ArrayList<AbstractTextComponent<?>>();
	private List<String> layouts = new ArrayList<String>();
	
	public VirtualKeyboard( String id, String layout ) {
		super( id );
		this.layouts.add( layout );
	}

	@Override
	@SuppressWarnings("unchecked")
	public void bind(Component component) {
		
		//todo check if abstract textcomponent
		this.textComponents.add( (AbstractTextComponent) component );
		component.setOutputMarkupId( true );		
	}

	public void addLayout( String layout ) {
		this.layouts.add( layout );
	}

	@Override
	public void renderHead(IHeaderResponse response) {
		super.renderHead( response );
		
		Map<String,Object> parameters = new HashMap<String,Object>();
		parameters.put( "id", this.getId() );
		
		StringBuilder builder = new StringBuilder();
		for( String layout : layouts ) {
			if( builder.length() > 0 ) {
				builder.append( ',' );
			}
			builder.append( "'" ).append( layout ).append( "'" );
		}
		parameters.put( "layouts", builder.toString() );

		builder = new StringBuilder();
		for( AbstractTextComponent<?> component : textComponents ) {
			if( builder.length() > 0 ) {
				builder.append( ',' );
			}
			builder.append( "'" ).append( component.getMarkupId() ).append( "'" );
		}
		parameters.put( "textfieldIds", builder.toString() );
		
		response.renderJavascript( this.getJavaScript( "VirtualKeyboard.js", parameters ), this.getId() );
	}

	public void setVisible( boolean visible, G4WicketEvent event ) {

		this.execute( this.getId() + ".setVisible(" + visible + ")", event );
	}
	
	public void setLayout( String layout, G4WicketEvent event ) {

		this.execute( this.getId() + ".setLayout(" + layout + ")", event );
	}
}
